//JObj.use('JDrag');
if(!JObj.Plugin) JObj.Plugin = {};
JObj.Plugin.JGrid = {};

(function($){

    var JGrid = function(width,height,body){
        var $ = this;

        var objs = {
            body: JObj.$(body) || document.body,
            outline:null,
            table:null,

            titleArea:null,
            labelArea:null,
            rowNumArea:null,
            dataArea:null,
            fp:document.createDocumentFragment(),
            colGroup:null,
            resizeLine:null,

            sortedField:null
        };
        var vars = {
            skin:'JGridDefault',
            title:null,
            titleHeight:30,
            lineNumWidth:30,
            totalWidth:0
        };

        $.setSkin = function(skin){
            vars.skin = skin;
            if(objs.outline) objs.outline.className = skin;
        }

        var dataArea_scroll = function(evt){
            objs.titleArea.scrollLeft = objs.dataArea.scrollLeft;            
        }

        var fieldLabel_click = function(){
            if(objs.table.rows.length < 2 ) return false;

            if(objs.sortedField != null){
                JObj.$tag("SPAN",objs.sortedField)[1].className = "";
            }

            objs.sortedField = this;
            JObj.$tag("SPAN",objs.sortedField)[1].className = "sort " + (this.sortMode ? "desc" : "asc");
            
            var tab_array = new Array();
            this.sortMode = (this.sortMode == undefined ? true : !this.sortMode);

            var tRow;
            for(var i = 0;tRow = objs.table.rows[i];i++){
                tab_array.push(new Array(objs.table.rows[i].cells[this.index].innerHTML.toLowerCase(),tRow));
            }

            var sortMode = this.sortMode;
            tab_array.sort(function (){
                                    var a = arguments[0][0] , b = arguments[1][0];
                                    //========================================
                                    if(/^(\+|-)?\d+($|\.\d+$)/.test(a) && /^(\+|-)?\d+($|\.\d+$)/.test(b)){
                                        a=eval(a);
                                        b=eval(b);
                                    }else{
                                        a=a.toString();
                                        b=b.toString();
                                    }

                                    return (sortMode ? (a>b ? 1:(a<b ? -1:0)) : (a<b ? 1:(a>b ? -1:0)));
                                    //========================================
                                }
                                );

            var tTbody = objs.table.tBodies[0];
            objs.fp.appendChild(tTbody);
            try{
                for(i=0;tTbody.appendChild(tab_array[i][1]);i++){};
            }catch(e){}
            objs.table.appendChild(tTbody);

            tTbody = null;
            tRow = null;
            delete tab_array;
        }
        //***************************************************************

        $.create = function(){
            objs.outline = JObj.$c('DIV');
            objs.body.appendChild(objs.outline);
            objs.outline.className = vars.skin;
            JObj.Style.css(objs.outline,{width:width,height:height});

            objs.titleArea = JObj.$c('DIV');
            objs.fp.appendChild(objs.titleArea);
            objs.titleArea.className = "titleArea";
            JObj.Style.css(objs.titleArea,{overflow:"hidden",paddingLeft:vars.lineNumWidth,height:vars.titleHeight});            

            objs.labelArea = JObj.$c("DIV");
            objs.titleArea.appendChild(objs.labelArea);
            objs.labelArea.className = 'labelArea';

            objs.dataArea = JObj.$c('DIV');
            objs.fp.appendChild(objs.dataArea);
            objs.dataArea.className = 'dataArea';
            JObj.addEvent(objs.dataArea,'onscroll',dataArea_scroll);
            JObj.Style.css(objs.dataArea,{marginLeft:vars.lineNumWidth,width:objs.outline.clientWidth - vars.lineNumWidth,height:objs.outline.clientHeight - vars.titleHeight,overflow:'auto'});

            //objs.outline.appendChild(objs.fp);
        }

        $.bindDataTable = function(tbl){
            objs.table = JObj.$(tbl);
            objs.table.removeAttribute('width');
            objs.table.removeAttribute('bordercolor');
            objs.dataArea.appendChild(objs.table);
        }

        /**
         *
         * @param object
         * [
         *  {label:'����',width:800,align:'right'},
         *  {label:'����',width:800,align:'right'}
         * ]
         */
        $.bindTitle = function(title){
            vars.title = title;
            objs.colGroup = JObj.$c("COLGROUP");

            var t,i,col,label,separator,txt,sort;
            for(i=0;t = title[i];i++){
                col = JObj.$c('COL');
                objs.colGroup.appendChild(col);
                JObj.Style.css(col,{width:t.width, textAlign:t.align || 'left'});

                label = JObj.$c('DIV');
                objs.labelArea.appendChild(label);
                label.className = 'label';
                label.index = i;/////////////////////
                label.onclick = fieldLabel_click;

                txt = JObj.$c('SPAN');
                label.appendChild(txt);
                txt.innerHTML = t.label;

                sort = JObj.$c('SPAN');
                label.appendChild(sort);

                JObj.Style.css(label,{width:(t.width + (JObj.Browser.ie && JObj.Browser.version < 8 ? 1 : 0 ))});

                separator = JObj.$c('DIV');
                objs.labelArea.appendChild(separator);
                separator.className = 'separator';
                vars.totalWidth += t.width + 2;
            }

            JObj.Style.css(objs.labelArea,{width:vars.totalWidth + 200});
            JObj.Style.css(objs.table, {width:vars.totalWidth});
            JObj.Style.addRule('.' + vars.skin + ' .titleArea .labelArea .label','line-height:' + vars.titleHeight + 'px;');
            JObj.Style.addRule('.' + vars.skin + ' .titleArea .labelArea .separator, .' + vars.skin + ' .titleArea .labelArea .label .sort','height:' + vars.titleHeight + 'px;');
            objs.outline.appendChild(objs.fp);
            objs.table.insertBefore(objs.colGroup,objs.table.tBodies[0]);
        }        
    }

    $.getInstance = function(width,height,body){
        JObj.Loader.loadCss(JObj.path + '/plugins/JGrid/default/default.css');
        var grid = new JGrid(width,height,body);
        grid.create();
        return grid;
    }

})(JObj.Plugin.JGrid);